﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using Table.Blo;
using UI;
using UnityEngine;
using Yunchang;
using Yunchang.NetworkLayer;

public class ChessWeeklyTaskItem : LadderDailyTaskItem
{
    public GameObject activeMonthCardBtn;
    public bool IsMonthCardTask
    {
        get;set;
    }

    protected override void UpdateTaskDesc(int taskId)
    {
        var dict = AutoChessBlo.GetTaskTableById(taskId);
        this.taskName.text = dict.taskName;
        this.taskDescription.text = dict.taskDesc;
    }

    protected override void UpdateTaskReward(int taskId)
    {
        var dict = AutoChessBlo.GetTaskTableById(taskId);
        var rewards = AutoChessBlo.GetTaskRewardById(taskId);
        if (rewards.Count > 0)
        {
            rewardIcon.SetData(rewards[0].id, rewards[0].type, rewards[0].num, true);
        }
        textureKit.TextureName = dict.taskIcon;
    }

    protected override void UpdateUITaskProgress(int taskId, long taskProgress)
    {
        var dict = AutoChessBlo.GetTaskTableById(taskId);
        var taskTarget = dict.taskTarget;
        if (taskProgress > taskTarget)
        {
            ExLog.Log("当前任务的进度超过了目标进度！将按目标进度进行显示");
            taskProgress = taskTarget;
        }
        this.taskProgress.text = string.Format("（{0}/{1}）", taskProgress, taskTarget);
    }

    protected override void UpdateUIBtnState()
    {
        cantReceive.SetActive(State == (int)AutoChessTaskState.DOING);  //未完成
        received.SetActive(State == (int)AutoChessTaskState.FINISH);    //已领取

        bool noMonthCardAndIsCardTask = ChessWeeklyTaskPage.HasChessMonthCard == false && IsMonthCardTask;

        bool reciveState = (State == (int)AutoChessTaskState.DONE) && !noMonthCardAndIsCardTask; //可领取
        receiveBtn.SetActive(reciveState);  //完成未领取

        bool activeCard = (State == (int)AutoChessTaskState.DONE) && noMonthCardAndIsCardTask;
        activeMonthCardBtn.SetActive(activeCard);
    }

    public override void OnClickReceiveBtn()
    {
        bool noMonthCardAndIsCardTask = ChessWeeklyTaskPage.HasChessMonthCard == false && IsMonthCardTask;
        var needActive = (State == (int)AutoChessTaskState.DONE) && noMonthCardAndIsCardTask;
        if (needActive)
        {
            PageManager.Instance.OpenPage("ChessCitationPage");
            return;
        }
        NetworkDriver.Instance.SendSocketBlock(OpDefine.CSGetAutoChessTaskReward, new CSGetAutoChessTaskReward()
        {
            taskId = this.TaskId
        }
        , OpDefine.SCGetAutoChessTaskReward);
    }

    public void OpenActiveMonthCard()
    {
        PageManager.Instance.OpenPage("ChessCitationPage");
    }
}
